package com.feifan.monthhelper.ui.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.StringUtils;
import com.feifan.monthhelper.R;
import com.feifan.monthhelper.calendar.CalendarUtils;
import com.feifan.monthhelper.constant.Constant;
import com.feifan.monthhelper.ui.MainActivity;
import com.feifan.monthhelper.ui.base.BaseActivity;
import com.feifan.monthhelper.utils.DateUtils;
import com.feifan.monthhelper.utils.NumberUtils;
import com.feifan.monthhelper.utils.SPUtils;
import com.feifan.monthhelper.wedget.DoubleClickListener;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

public class Step1Activity extends BaseActivity {

    @BindView(R.id.rl_title)
    View rlTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_logo)
    ImageView ivLogo;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_menses_date_cycle)
    TextView tvMensesDateCycle;
    @BindView(R.id.tv_menses_start_date)
    TextView tvMensesStartDate;
    @BindView(R.id.tv_to)
    TextView tvTo;
    @BindView(R.id.tv_menses_end_date)
    TextView tvMensesEndDate;
    @BindView(R.id.tv_next)
    TextView tvNext;

    TimePickerView pvTime;

    //开始时间回调函数
    OnTimeSelectListener startOnTimeSelectListener = new OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {
            String dataString= DateUtils.init().dateToString(date, DateUtils.DateStyle.YYYY_MM_DD);
            tvMensesStartDate.setText(dataString);
        }
    };

    //结束时间回调函数
    OnTimeSelectListener endOnTimeSelectListener = new OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {
            String dataString= DateUtils.init().dateToString(date, DateUtils.DateStyle.YYYY_MM_DD);
            tvMensesEndDate.setText(dataString);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View getTitleView() {
        return rlTitle;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        tvMensesStartDate.setText(SPUtils.getInstance().getString(Constant.ShareKey.MENSTRUAL_CYCLE_START_DATE));
        tvMensesEndDate.setText(SPUtils.getInstance().getString(Constant.ShareKey.MENSTRUAL_CYCLE_END_DATE));

        tvMensesStartDate.setOnClickListener(new DoubleClickListener() {
            @Override
            public void doOnClick(View view) {
                showDateSelectDialog(tvMensesStartDate.getText().toString().trim(), Step1Activity.this, startOnTimeSelectListener);

            }
        });
        tvMensesEndDate.setOnClickListener(new DoubleClickListener() {
            @Override
            public void doOnClick(View view) {
                showDateSelectDialog(tvMensesEndDate.getText().toString().trim(), Step1Activity.this, endOnTimeSelectListener);

            }
        });
        tvNext.setOnClickListener(new DoubleClickListener() {
            @Override
            public void doOnClick(View view) {
                String startDate = tvMensesStartDate.getText().toString().trim();
                String endDate = tvMensesEndDate.getText().toString().trim();
                if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
                    showToast(R.string.menstruation_cycle_date_is_null);
                    return;
                }
                if(DateUtils.init().StringToLong(startDate, DateUtils.DateStyle.YYYY_MM_DD) >= DateUtils.init().StringToLong(endDate, DateUtils.DateStyle.YYYY_MM_DD)) {
                    showToast(R.string.menstruation_start_date_is_late);
                    return;
                }
                SPUtils.getInstance().put(Constant.ShareKey.MENSTRUAL_CYCLE_START_DATE, startDate);
                SPUtils.getInstance().put(Constant.ShareKey.MENSTRUAL_CYCLE_END_DATE, endDate);
                startActivity(new Intent(Step1Activity.this, Step2Activity.class));
            }
        });
    }

    private void showDateSelectDialog(String dateString, AppCompatActivity activity, OnTimeSelectListener onTimeSelectListener) {
//        String dateString = "";
        Calendar selectedDate = CalendarUtils.getCalendarFrom(dateString);

        pvTime = new TimePickerBuilder(activity, onTimeSelectListener)
                .setType(new boolean[]{true, true, true, false, false, false})//分别对应年月日时分秒，默认全部显示
                .setCancelText("取消")//取消按钮文字
                .setSubmitText("确定")//确认按钮文字
                .setContentTextSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
//                .setTitleText("Title")//标题文字
                .setOutSideCancelable(false)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(true)//是否循环滚动
//                .setTitleColor(Color.BLACK)//标题文字颜色
//                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
//                .setCancelColor(Color.BLUE)//取消按钮文字颜色
//                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
//                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
//                .setRangDate(Calendar.getInstance().get(Calendar.YEAR) - 20, Calendar.getInstance().get(Calendar.YEAR) + 20)//默认是1900-2100年
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
//                .setRangDate(startDate,endDate)//起始终止年月日设定
                .setLabel("年","月","日","时","分","秒")
                .isDialog(false)//是否显示为对话框样式
                .build();
        pvTime.show();
    }

    @Override
    protected int setContentLayout() {
        return R.layout.activity_step1;
    }
}
